package com.lucy.nepu.module.paper.rule;

import cn.hutool.core.lang.Validator;
import com.lucy.nepu.loader.PeriodLoader;
import com.lucy.nepu.module.htopic.constant.HorTopicTypeConst;
import com.lucy.nepu.module.htopic.rule.IsHorTopicType;
import com.lucy.nepu.module.paper.entity.Paper;
import com.lucy.nepu.system.period.Period;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class InPeriodValidator implements ConstraintValidator<InPeriod, Object> {
    @Autowired(required = false)
    PeriodLoader periodLoader;

    @Override
    public void initialize(InPeriod constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        Paper paper = (Paper) o;
        Period period = periodLoader.getPeriod();
        if (paper.getPublicationTime().before(period.getStartDate())||
                paper.getPublicationTime().after(period.getEndDate())) {
            return false;
        }
        return true;
    }
}